'use strict';
var jwt = require('jsonwebtoken');
const Controller = require('egg').Controller;

const gData = [
  {
    id: '00000001',
    brand: '文具',
    price: '89.00',
    priceRange: '50-100',
    type: '1',
    description: '新华定制 新华英雄钢笔，亲历经典情怀，书写时代精神',
    comment: '26',
    image: 'http://img7.xinhuashudian.com/images/2020/12/03/7768f70f-7a97-4297-a5e9-2947122de243.jpg?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
  },
  {
    id: '00000002',
    brand: '纪念币',
    price: '199.00',
    priceRange: '100-200',
    type: '1',
    description: '限量发行百年辉煌纪念币整版票珍藏集，不忘初心，牢记使命',
    comment: '466',
    image: 'https://img7.xinhuashudian.com/images/2021/09/24/ee4f9634-1cd2-461d-a1a4-ca5ce103dbf3.jpg?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
  },
  {
    id: '00000003',
    brand: '桌游',
    price: '99.00',
    priceRange: '50-100',
    type: '2',
    description: '新华定制 《强国梦》文创桌游，让参与者在游戏娱乐中高效理解掌握党的思想政治理论知识的丰富内涵',
    comment: '1008',
    image: 'https://img3.xinhuashudian.com/images/2020/07/30/e55edfe3-9b9e-4bd5-bbef-733f8defc0f4.png?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp/watermark,image_aW1hZ2VzLzIwMTgvMDcvMjEvNTc4N2Q2YWMtNGU1YS00NzQxLTkzNWUtYWY3NDk1Mzc3OWE3LnBuZw==,t_50,g_center'
  },
  {
    id: '00000004',
    brand: '文具',
    price: '32.00',
    priceRange: '50以下',
    type: '1',
    description: '韵文博鉴 我爱中国字母笔 一盒七支，士不可以不弘毅，任重而道远，当从一笔一划开始积累，建设祖国',
    comment: '1万+',
    image: 'https://img2.xinhuashudian.com/images/2021/06/02/643f021a-6acf-49a6-8d9b-2b736ab48d16.jpg?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
  },
  {
    id: '00000005',
    brand: '数码',
    price: '160.00',
    priceRange: '100-200',
    type: '2',
    description: '“红色圣地”延安宝塔山折叠无线充电鼠标垫，象征着中华名族坚强不屈、英勇抗敌的勇气',
    comment: '200+',
    image: 'https://img9.xinhuashudian.com/images/2021/08/25/e118c713-ca84-4aff-9de5-4a812aa4dbf2.jpg?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
  },
  {
    id: '00000006',
    brand: '积木',
    price: '351.00',
    priceRange: '200-500',
    type: '2',
    description: '【国家名胜文化经典】黄鹤楼建筑积木，独特的中华文化',
    comment: '2701',
    image: 'https://img3.xinhuashudian.com/images/2021/04/14/ea45d156-fe81-4914-96be-524b98b4cd79.jpg?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
  },
  // {
  //   id: '00000007',
  //   brand: '生活用品',
  //   price: '126.00',
  //   priceRange: '100-200',
  //   type: '1',
  //   description: '延安中国红色书店 中国红色书店茶具套盒，延安精神永久流传',
  //   comment: '5000+',
  //   image: '//img4.xinhuashudian.com/images/2019/05/09/c5ff8a1d-0429-476e-a96d-a36fa15945a2.png?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
  // },
  {
    id: '00000008',
    brand: '生活用品',
    price: '189.00',
    priceRange: '100-200',
    type: '1',
    description: '「海水江崖纹」金丝珐琅彩杯 云作好物，中华传统纹样图案之一',
    comment: '1000+',
    image: 'https://img7.xinhuashudian.com/images/2020/12/29/eac95926-324d-4d49-9682-56d8742c8f8f.jpg?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp/watermark,image_aW1hZ2VzLzIwMTgvMDcvMjEvNTc4N2Q2YWMtNGU1YS00NzQxLTkzNWUtYWY3NDk1Mzc3OWE3LnBuZw==,t_50,g_center'
  }
];

class HomeController extends Controller {
  async index() {
    this.ctx.body = 'hi, egg';
  }

  async phoneList() {
    let { brand, price, type, page } = this.ctx.query;
    console.log('page------', page)

    let data = gData;
    if (page === '2') {
      data = []
      for (let i = 0; i < 8; i++) {
        data.push({
          id: '00000007',
          brand: '生活用品',
          price: '126.00',
          priceRange: '100-200',
          type: '1',
          description: '延安中国红色书店 中国红色书店茶具套盒，延安精神永久流传',
          comment: '5000+',
          image: '//img4.xinhuashudian.com/images/2019/05/09/c5ff8a1d-0429-476e-a96d-a36fa15945a2.png?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
        })
      }
    }
    if (page === '3') {
      data = []
      for (let i = 0; i < 8; i++) {
        data.push({
          id: '00000006',
          brand: '积木',
          price: '351.00',
          priceRange: '200-500',
          type: '2',
          description: '【国家名胜文化经典】黄鹤楼建筑积木，独特的中华文化',
          comment: '2701',
          image: 'https://img3.xinhuashudian.com/images/2021/04/14/ea45d156-fe81-4914-96be-524b98b4cd79.jpg?x-oss-process=image/resize,m_mfit,h_750,w_750/format,webp'
        })
      }
    }
    console.log(data)
    this.ctx.body = data.filter((item) => {
      // console.log(item[type], key)
      return (!brand || item.brand === brand) && (!price || item.priceRange === price) && (!type || item.type === type);
    })
  }

  async banner() {
    this.ctx.body = [
      {
        id: '00000006',
        image: 'https://img6.xinhuashudian.com/images/2019/05/22/88203bad-056e-4c7a-b3dd-980e2fc6d8a4.jpg?x-oss-process=image/format,webp'
      },
      {
        id: '00000003',
        image: 'https://img5.xinhuashudian.com/images/2021/09/13/e88ba390-8d60-4a29-a762-33262a94e305.jpg?x-oss-process=image/format,webp'
      },
      {
        id: '00000008',
        image: 'https://img4.xinhuashudian.com/images/2021/08/02/65295933-0395-4b5d-bd91-6453409d1770.png?x-oss-process=image/format,webp'
      }
    ];
  }

  async filterMenu() {
    const { userName, passWord } = this.ctx.request.body;
    this.ctx.body = {
      brand: ['文具', '数码', '桌游', '纪念币', '生活用品', "积木"],
      price: ['50以下', '50-100', '100-200', '200-500', '500以上'],
      type: ['自营', '第三方']
    };
  }

  async pay() {
    this.ctx.body = {
      retcode: 1,
      msg: '结算成功'
    }
  }

  async addCart() {

    // for(let i=31; i<=40; i++)
    // await this.ctx.model.User.create({
    //     name: 'a' + i,
    //     password: '123456'
    //   })

    const token = this.ctx.cookies.get("token", {
      signed: false,
      encrypt: true
    });
    const decoded = jwt.verify(token, "aaa");
    const { id } = this.ctx.request.body;

    const res = await this.ctx.model.Pccart.findOne({ id: id, userId: decoded.id });

    if (res) {
      const re = await this.ctx.model.Pccart.updateOne(
        { id: id, userId: decoded.id },
        { num: res.num + 1 }
      );
      this.ctx.body = {
        code: 1,
        msg: 'ok',
      };
    } else {
      const item = this.ctx.request.body;;
      if (item) {
        await this.ctx.model.Pccart.create({
          id: item.id,
          description: item.description || item.brand + item.type + item.color,
          image: item.image,
          price: item.price,
          num: 1,
          userId: decoded.id
        })
        this.ctx.body = {
          code: 1,
          msg: 'ok',
        };
      } else {
        this.ctx.body = {
          code: -1,
          msg: 'error',
        };
      }
    }
  }

  async getCart() {
    const token = this.ctx.cookies.get("token", {
      signed: false,
      encrypt: true
    });
    const decoded = jwt.verify(token, "aaa");
    const res = await this.ctx.model.Pccart.find({ userId: decoded.id })
    let totalPrice = 0
    res.map((item) => {
      totalPrice += item.price * item.num
    })
    this.ctx.body = {
      code: 0,
      msg: 'ok',
      data: {
        cartList: res,
        totalPrice
      }
    };
  }

  // 删除购物车
  async deleteCart() {
    const { id } = this.ctx.request.body;
    const token = this.ctx.cookies.get("token", {
      signed: false,
      encrypt: true
    });
    const decoded = jwt.verify(token, "aaa");
    const res = await this.ctx.model.Pccart.remove({
      id,
      userId: decoded.id
    });
    this.ctx.body = {
      code: 1,
      msg: 'ok',
    };
  }

  // 获取购物车计数
  async getCartNum() {
    const token = this.ctx.cookies.get("token", {
      signed: false,
      encrypt: true
    });
    const decoded = jwt.verify(token, "aaa");
    const res = await this.ctx.model.Pccart.find({ userId: decoded.id }, { num: 1, _id: 0 });
    let count = 0
    res.map((item) => {
      count += item.num
    })
    this.ctx.body = {
      code: 1,
      msg: 'ok',
      data: count
    };
  }

  // 修改购物车计数
  async changeNum() {
    const token = this.ctx.cookies.get("token", {
      signed: false,
      encrypt: true
    });
    const decoded = jwt.verify(token, "aaa");
    const { id, num } = this.ctx.request.body;
    const res = await this.ctx.model.Pccart.findOne({ id: id, userId: decoded.id });
    if (res) {
      const re = await this.ctx.model.Pccart.updateOne(
        { id: id, userId: decoded.id },
        { num: num }
      );
      this.ctx.body = {
        code: 1,
        msg: 'ok',
      };
    } else {
      this.ctx.body = {
        code: -1,
        msg: 'error'
      };
    }
  }
}

module.exports = HomeController;
